
package ueb13.htw.queue;

import java.util.List;
import ueb13.htw.list.DList;

/**
 * 
 * 
 * @author Andreas Dier, Steffen Schuhmann
 * @version 0.2
 */
public class PriorityQueue {
    
    private DList listobject = null;


    public PriorityQueue() {
        listobject = new DList();
    }
    
    /**
     * returns the last Element in the Queue
     * 
     * @return the last element in the queue
     */
    public Object getLast(){
        return listobject.getLast();
    }
    
    /**
     * returns the first Element in the Queue
     * 
     * @return the first element in the Queue
     */
    public Object getFirst(){
        return listobject.getFirst();
    }
   
    /**
     * returns the object at the given index
     * 
     * @param index of the object
     * @return the object at the given index
     */
    public Object getObject(int index){
        return listobject.get(index);
    }
    
    /**
     * adds an object in the right order
     * 
     * @param o object to add
     */
    public void add(Object o){
        boolean inserted = false;
        
        for(int i = 0; i <= listobject.size() || inserted == true ; i++ ){
            if(((Comparable) o).compareTo(listobject.get(i)) >= 1){
                listobject.add(i, o);
                inserted = true;
            }
        }
    }
    
   /**
    * removes the first element and returns it
    * 
    * @return the first element 
    */ 
   public Object removeFirst(){
       Object o = getFirst();
       listobject.removeFirst();
       return o;
   }
   
   /**
    * removes the last element and returns it 
    * 
    * @return the last element
    */
   public Object removeLast(){
       Object o = getLast();
       listobject.removeLast();
       return o;
   }
   
   protected Object remove(Object o){
       listobject.remove(o);
       return o;
   }
   
   /**
    * changes the priority of the given object
    * 
    * @param o object of which the priority should be changed
    * @param priority to which the element should be changed
    * @return the changed element
    */
   public Object changePriority(Object o, int priority){
       remove(o);
       ((Prioritizable) o).setPriority(priority);
       add(o);
       return o;
       
   }

 
   
   



    
}
